'''中升月度数据拉取'''
import os
import pandas as pd
import datetime
import sql_exe2 as sql
from env import ROOT_PATH
from utils import lastday_of_lastmonth

def prepare_data(start_t,end_t):
    '''根据起始终止日期读入该时段数据'''
    print(f'======={start_t}~{end_t}数据提取中=======')
    sql_all = open(os.path.join(ROOT_PATH, r"sql_file\sql_all.txt"), encoding='utf-8').read().replace('endtime',end_t).replace('starttime',start_t)
    #获取查询结果数据和表头
    data= pd.DataFrame(sql.select_with_header(sql_all))
    print('=======数据提取完成=======')
    return data


if __name__ == '__main__':

    is_monthly = True # True for fetching data and save by month

    # # IF run on your local PC for the first time, to get the raw data from 2023
    # if is_monthly:
    #   for i in range(1,13):  
    #     today = datetime.date(2023,i,3)#datetime.date.today()

    if is_monthly:
        today = datetime.date(2024,1,3)#datetime.date.today()#返回系统当日日期,默认每月初前五个工作日内完成数据拉取/制表 

        first_date, last_date = lastday_of_lastmonth(today)

        data_path = os.path.join(ROOT_PATH,'data_raw')
        if not os.path.exists(data_path):
            os.makedirs(data_path)

        file_name = f"{last_date}.xlsx"
        year_path = os.path.join(data_path,last_date[:4])
        if not os.path.exists(year_path):
            os.makedirs(year_path)

        file_path = os.path.join(year_path,file_name)

        # if not os.path.exists(file_path):
        data_raw = prepare_data(first_date,last_date)
        data_raw.to_excel(file_path, index=True,header=None)
